<template>
  <a-card title="todo" style="margin: 18px">
   <a-row :gutter="16" type="flex">
     <a-col :span="8">
       <a-input v-model:value="state.todoValue" placeholder="请输入todo" />
     </a-col>
     <a-button type="primary" @click="addTodo">添加</a-button>
   </a-row>

    <a-col :span="8" v-if="state.todoList.length">
      <ul class="listBox">
        <li class="listItem" v-for="(item, index) in state.todoList">
          <p>{{item}}</p>
          <a-button type="danger" @click="delTodo(index)">删除</a-button>
        </li>
      </ul>
    </a-col>
  </a-card>
</template>

<script>
  import { reactive  } from 'vue'
  export default {
    name: "todo",
    setup () {

      const state = reactive({
        todoValue: '',
        todoList: []
      })

      function addTodo() {
        if (state.todoValue !== '') {
          state.todoList.push(state.todoValue)
        }
      }

      function delTodo(index) {
        state.todoList.splice(index, 1)
      }

      return {
        state,
        addTodo,
        delTodo
      }
    }
  }
</script>

<style scoped>
  .listBox{
    box-sizing: border-box;
    border-radius: 2px;
    margin-top: 20px;
    border: 1px solid #e5e5e5;
    padding: 4px;
  }
  .listItem{
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid cornflowerblue;
  }
  .listItem > p{
    width: calc(100% - 70px);
    word-wrap: break-word;
  }
</style>
